<template>
	<view class="content">
		<view class="obtaincode">
			<view class="obtaincode-nav">欢迎登录红麦</view>
			<view class="obtaincode-con">
				<view class="obtaincode-phone">
					<text class="obtaincode-phone-text">请输入手机号码</text>
					<input
						class="py-16"
						border="bottom"
						v-model="value.phonevalue"
						:focus="true"
						type="number"
						:maxlength="11"
					/>
					<view class="divider"></view>
				</view>
				<view class="fd-r jc-c py-12">
					<tm-checkbox @change="accredit"></tm-checkbox>

					<view class="clontext flex1 fs-24">
						我已阅读并同意
						<view
							class="c-primary"
							@click="
								jumpShowPFD(
									'https://fdfs.michain.tech/group1/M00/00/31/rBJdlWTTU4aAZZFSAAPI3HbaSQ4530.pdf'
								)
							"
						>
							《用户协议》
						</view>
						和
						<view
							class="c-primary"
							@click="
								jumpShowPFD(
									'https://fdfs.michain.tech/group1/M00/00/31/rBJdlWTTU3-AYovHAAPI24Ewaic736.pdf'
								)
							"
						>
							《红麦咨询隐私政策》
						</view>
					</view>
				</view>
			</view>
			<view
				class="obtaincode-btn"
				@click="loginbtn"
				:class="value.phonevalue.length == 11 ? 'phonechange_btn' : ''"
			>
				获取验证码
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="thirdparty">
				<text class="thirdparty-text">其他方式登录</text>
				<view class="thirdparty-icon" @click="alipayLogin">
					<image
						src="@/Index/static/Alipay.png"
						mode=""
						class="thirdparty-another"
					></image>
				</view>
			</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
// import { interAspect, loginByCode, aliPayLogin } from '@/api/authApi.js';

export default {
	data() {
		return {
			value: {
				//手机号绑定值
				phonevalue: '',
				//验证码
				phonecodevalue: ''
			},

			// 486707
			//验证码的变量

			//用户授权
			isaccredit: false
		};
	},

	methods: {
		accredit(e) {
			this.isaccredit = e;
		},
		//手机号框失去焦点时验证
		phonenumberblur() {
			if (this.value.phonevalue.length != 11) {
				uni.$u.toast('手机号长度不正确');
			}
		},
		//验证码方法
		phonecodeblur(e) {
			if (this.value.phonecodevalue.length != 6) {
				uni.$u.toast('验证码长度不正确');
			}
		},
		//点击登录
		loginbtn() {
			if (this.isaccredit) {
				if (this.value.phonevalue.length != 11)
					return uni.$u.toast('请检查手机号长度');

				if (
					/^[1][3,4,5,6.7,8,9][0-9]{9}$/.test(this.value.phonevalue)
				) {
					uni.showLoading({
						title: '正在获取验证码'
					});
					uni.navigateTo({
						url: `/pages/login/code/index?phonenumber=${this.value.phonevalue}`
					});
					
					interAspect({
						phone: this.value.phonevalue
					})
						.then((res) => {
							uni.showToast({
								icon: 'success',
								title: res.data
							})
							uni.hideLoading();
							uni.navigateTo({
								url: `/pages/login/code/index?phonenumber=${this.value.phonevalue}`
							});
						})
						.catch((err) => {});
				} else {
					uni.$u.toast('请输入正确手机号');
					return;
				}
			} else {
				uni.$u.toast('请先授权');
			}
		},

		//倒计时结束
		end() {
			this.changeText = false;
		},
		//第三方之微信登录
		alipayLogin() {
			if (!this.isaccredit) {
				return uni.$u.toast('请授权');
			}
			let urls =
				'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2021004108684267&scope=auth_user&redirect_uri=https://applet.xycloud.net.cn/hongmai-applet/static/redirect_uri.html';
			urls = encodeURIComponent(urls); //将地址编码成浏览器访问的格式
			// 判断平台
			if (plus.os.name == 'Android') {
				plus.runtime.openURL(
					'alipays://platformapi/startapp?appId=20000067&url=' + urls,
					(res) => {
						//这里写打开URL地址失败后的处理
						console.log(res);
						uni.showModal({
							content:
								'本机未检测到对应客户端，请下载支付宝后重试',
							success: function (res) {
								if (res.confirm) {
									//plus.runtime.openURL();
								}
							}
						});
					},
					'com.eg.android.AlipayGphone'
				);
			}
		},
		//打开pdf阅读
		jumpShowPFD(value) {
			uni.showLoading({
				title: '加载中'
			});
			uni.downloadFile({
				url: value,
				success: function (res) {
					var filePath = res.tempFilePath;
					uni.openDocument({
						filePath: filePath,
						showMenu: true,
						fileType: 'pdf',
						success: function (res) {
							uni.hideLoading({});
							console.log('打开文档成功');
							uni.getFileSystemManager().removeSavedFile({
								filePath: filePath,
								success() {
									console.log('已删除文件：' + filePath);
								}
							});
						},
						fail(err) {
							console.log(err);
							uni.hideLoading({});
						}
					});
				}
			});
		}
	},
	async onShow() {
		// #ifdef APP-PLUS
		let args = plus.runtime.arguments;

		if (args) {
			uni.showLoading({});
			plus.runtime.arguments = null;
			let authCode = args.split('=')[1];
			console.log(authCode);
			if (authCode != undefined && authCode != '' && authCode != null) {
				const LoginResult = await aliPayLogin({
					code: authCode
				});
				if (LoginResult.status === 200) {
					console.log(LoginResult);
					if (LoginResult.message && LoginResult.message != null)
						uni.setStorageSync('token', LoginResult.message);
					if (LoginResult.data && LoginResult.data != null)
						uni.setStorageSync(
							'userInfo2',
							JSON.stringify(LoginResult.data.data)
						);

					uni.switchTab({
						url: '/pages/index/index'
					});
					uni.$u.toast('登录成功');
				} else {
					uni.$u.toast('登录失败');
				}
			} else {
				uni.$u.toast('请重新授权');
			}
		}

		// #endif
	}
};
</script>

<style lang="scss" scoped>
.btntext {
	height: 48rpx;
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: rgba(0, 0, 0, 0.4);
	line-height: 48rpx;
	position: relative;
}

.btntext::before {
	position: absolute;
	content: '';
	background-color: #e3e6eb;
	// background-color: red;
	left: -24rpx;
	top: 12rpx;
	width: 4rpx;
	height: 24rpx;
}

.changetext {
	color: #1664ff;
}

.content {
	width: 750rpx;
	height: 100vh;
	padding: 150rpx 48rpx 0;
	box-sizing: border-box;
	// background: #FFFFFF;

	.obtaincode {
		.obtaincode-nav {
			height: 66rpx;
			font-size: 48rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 900;
			color: #000000;
			line-height: 66rpx;
			margin-bottom: 130rpx;
		}

		.obtaincode-con {
			.obtaincode-phone {
				margin-bottom: 10rpx;

				.obtaincode-phone-text {
					height: 40rpx;
					font-size: 32rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: rgba(0, 0, 0, 0.26);
					line-height: 40rpx;
				}
			}

			.agreement {
				display: flex;
				justify-content: center;
				margin-bottom: 24rpx;
			}
		}

		.obtaincode-btn {
			width: 100%;
			height: 84rpx;
			background: #f1949e;
			border-radius: 42rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			font-weight: 700;
			color: rgba(255, 255, 255, 0.9);
		}

		.phonechange_btn {
			background: #d7000f;
		}

		.thirdparty {
			position: fixed;
			bottom: 140rpx;
			left: 50%;
			transform: translateX(-50%);

			.thirdparty-text {
				width: 100%;
				text-align: center;

				height: 40rpx;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: rgba(0, 0, 0, 0.4);
				line-height: 40rpx;
			}

			.thirdparty-icon {
				margin-top: 40rpx;
				width: 100%;
				display: flex;
				justify-content: center;

				.thirdparty-another {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
				}
			}
		}
	}
}

.clontext {
	display: flex;
	height: 80rpx;
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: rgba(0, 0, 0, 0.9);
	line-height: 80rpx;
}
</style>
